package www.openteam.com.adapter.other;

import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.util.SortedList;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import www.openteam.com.R;
import www.openteam.com.bean.CustomConditon;
import www.openteam.com.bean.CustomType;
import www.openteam.com.bean.PackTypeInfo;

/**
 * Created by kylin on 2016/11/25.
 */

public class CustomPMCInfoAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * 产品资料适配器
     */
    private final String TAG = CustomPMCInfoAdapter.class.getSimpleName();
    private LayoutInflater mLayoutInflater;
    private SortedList<CustomType.DataBean> mSortedList;
    private CButtonTouchEvent cButtonTouchEvent;
    private Context context;

    public CustomPMCInfoAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
//        Log.d(TAG, "DataAdapter: 1");
        mSortedList = new SortedList<>(CustomType.DataBean.class, new SortedList.Callback<CustomType.DataBean>() {

            /**
             * 返回一个负整数（第一个参数小于第二个）、零（相等）或正整数（第一个参数大于第二个）
             */
            @Override
            public int compare(CustomType.DataBean o1, CustomType.DataBean o2) {

                if (Integer.parseInt(o1.getCode()) < Integer.parseInt(o1.getCode())) {
                    return -1;
                } else if (Integer.parseInt(o1.getCode()) > Integer.parseInt(o1.getCode())) {
                    return 1;
                }

                return 0;
            }

            @Override
            public boolean areContentsTheSame(CustomType.DataBean oldItem, CustomType.DataBean newItem) {
                return oldItem.getCode().equals(oldItem.getCode());
            }

            @Override
            public boolean areItemsTheSame(CustomType.DataBean item1, CustomType.DataBean item2) {
                boolean statue;
                if (item1.getCode() == item2.getCode()
                       ) {
                    statue = true;
                } else {
                    statue = false;
                }

                return statue;
            }

            @Override
            public void onInserted(int position, int count) {
                notifyItemRangeInserted(position, count);
            }

            @Override
            public void onRemoved(int position, int count) {
                notifyItemRangeRemoved(position, count);
            }

            @Override
            public void onMoved(int fromPosition, int toPosition) {
                notifyItemMoved(fromPosition, toPosition);
            }

            @Override
            public void onChanged(int position, int count) {
                notifyItemRangeChanged(position, count);
            }
        });
    }

    public void addItems(ArrayList<CustomType.DataBean> list) {
        mSortedList.beginBatchedUpdates();
        for (CustomType.DataBean itemModel : list) {
            mSortedList.add(itemModel);
        }
        mSortedList.endBatchedUpdates();
    }
    public void addContext(Context context){
        this.context=context;
    }

    public void deleteItems(ArrayList<CustomType.DataBean> items) {
        mSortedList.beginBatchedUpdates();
        for (CustomType.DataBean item : items) {
            mSortedList.remove(item);
        }
        mSortedList.endBatchedUpdates();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        Log.d(TAG, "DataAdapter: 3");
        return new CustomPMCInfoAdapter.ViewHolder(mLayoutInflater.inflate(R.layout.item_base_no_audit, parent, false),context);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//        Log.d(TAG, "DataAdapter:4");
        final CustomType.DataBean data = mSortedList.get(position);

        CustomPMCInfoAdapter.ViewHolder viewHolder = (CustomPMCInfoAdapter.ViewHolder) holder;
        viewHolder.textView.setText("编号:" + data.getCode() + "");

        viewHolder.tv_title.setText(
                        "币别:" + data.getName() + "\n"
        );

        viewHolder.buttonSetOnclick(new ViewHolder.ButtonTouchEvent() {
            @Override
            public void doubleClickSelect() {
//                Log.d(TAG, "doubleClickSelect: "+data.getPbName());
                cButtonTouchEvent.doubleClickSelect(data);
            }
        });

    }

    public interface  CButtonTouchEvent{
        void  doubleClickSelect(CustomType.DataBean packTypeInfo);
    }

    /**
     * 按钮点击事件需要的方法
     */
    public void cButtonSetOnclick(CButtonTouchEvent cButtonTouchEvent) {
        this.cButtonTouchEvent = cButtonTouchEvent;
    }


    @Override
    public int getItemCount() {

//        Log.d(TAG, "getItemCount: " +mSortedList.size());
        return mSortedList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public GestureDetectorCompat gestureDetectorCompat;
        private TextView textView;
        private TextView tv_title;
        private CardView cardView;
        private ButtonTouchEvent buttonTouchEvent;
        private MyGestureListener myGestureListener =new MyGestureListener();
        public ViewHolder(View itemView,Context context) {
            super(itemView);
//            Log.d(TAG, "DataAdapter: 5");
            cardView = (CardView) itemView.findViewById(R.id.card);
            textView = (TextView) itemView.findViewById(R.id.tv_id);
            tv_title = (TextView) itemView.findViewById (R.id.tv_title);
             gestureDetectorCompat =new GestureDetectorCompat(context,new MyGestureListener());
            cardView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    gestureDetectorCompat.onTouchEvent(event);
                    return true;
                }
            });


        }

        ;

        public class MyGestureListener extends SimpleOnGestureListener {
            private String TAG = MyGestureListener.class.getSimpleName();

            @Override
            public boolean onDoubleTap(MotionEvent e) {

//                if (buttonTouchEvent != null){
                    buttonTouchEvent.doubleClickSelect();
                    Log.d(TAG, "onDoubleTapEvent: ");
//                }
                return false;
            }


        }


        public void buttonSetOnclick(ButtonTouchEvent buttonTouchEvent) {
            this.buttonTouchEvent = buttonTouchEvent;
        }

        /**
         * 按钮点击事件需要的方法
         */
        public interface  ButtonTouchEvent{
            void  doubleClickSelect();
        }

    }



}
